package cn.gs.serializable.serialize;

// @author gs
import cn.gs.serializable.entity.IEntity;
import cn.gs.serializable.entity.OneEntity;

public class EnumSerialize implements ISerialize {

    @Override
    public <T> T toObj(cn.gs.serializable.serialize.ISerialize ctrl, IEntity from, Class<T> clazz) {
        if (!(from instanceof OneEntity)) {
            return null;
        }
        String t = ((OneEntity) from).getValue();
        if (clazz != null && clazz.isEnum()) {
            try {
                return (T) Enum.valueOf((Class) clazz, t);
            } catch (IllegalArgumentException ex) {
            }
        }
        return null;
    }

    @Override
    public IEntity fromObj(ISerialize ctrl, Object o) {
        if (o == null) {
            return null;
        }
        if (o instanceof Enum) {
            return new OneEntity(o.toString());
        } else {
            return null;
        }
    }
}
